package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.TagService;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.TagAddVO;
import com.junzhiit.nengji.cms.web.vo.TagEditVO;
import com.junzhiit.nengji.cms.web.vo.TagListVO;
import com.junzhiit.nengji.dao.TagDAO;
import com.junzhiit.nengji.model.Tag;

@Service
public class TagServiceImpl extends BaseServiceImpl implements TagService{

	@Autowired
	private TagDAO tagDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, TagListVO tagListVO){
		Tag tag=new Tag();
		BeanUtils.copyProperties(tagListVO, tag);
		
		List<Tag> list = tagDAO.findAll(tag, dtp.getStart(), dtp.getLength());
		int totalCount = tagDAO.getTotalCount(tag);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		Tag tag=tagDAO.findById(id);
		
		result.setData(tag);
		return result;
	}
	
	@Override
	public ResultVO add(TagAddVO tagAddVO){
		ResultVO result=new ResultVO();
		Tag tag=new Tag();
		BeanUtils.copyProperties(tagAddVO, tag);
		Tag data=tagDAO.insert(tag);
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO edit(TagEditVO tagEditVO){
		ResultVO result=new ResultVO();
		
		Tag tag=new Tag();
		BeanUtils.copyProperties(tagEditVO, tag);
		tagDAO.update(tag);
		
		return result;
	}
	
	@Override
	public ResultVO updateToDelete(String id){
		ResultVO result=new ResultVO();
		
		tagDAO.updateToDelete(id);
		
		return result;
	}
	
	@Override
	public ResultVO getAll(TagListVO tagListVO){
		ResultVO result=new ResultVO();
		
		Tag tag=new Tag();
		BeanUtils.copyProperties(tagListVO, tag);
		
		List<Tag> data=tagDAO.getAll(tag);
		result.setData(data);
		
		return result;
	}
	
}
